﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace HenhouseEnvironmentCtrl
{
    public partial class ManageHenhouseForm : Form
    {
        public ManageHenhouseForm()
        {
            InitializeComponent();
        }

        private void ManageHenhouseForm_Load(object sender, EventArgs e)
        {
            LoadHenhouses();
        }

        private void LoadHenhouses()
        {
            using (SqlConnection conn = DBConnection.CreateConnection())
            {
                try
                {
                    DBConnection.Open(conn);
                    SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM Henhouse", conn);
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    dgvHenhouses.DataSource = dt;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("加载鸡舍数据时发生错误: " + ex.Message);
                }
                finally
                {
                    DBConnection.Close(conn);
                }
            }
        }

        private void btnAddHenhouse_Click(object sender, EventArgs e)
        {
            AddHenhouseForm addForm = new AddHenhouseForm();
            addForm.ShowDialog();
            LoadHenhouses();
        }

        private void btnEditHenhouse_Click(object sender, EventArgs e)
        {
            if (dgvHenhouses.SelectedRows.Count > 0)
            {
                int henhouseId = Convert.ToInt32(dgvHenhouses.SelectedRows[0].Cells["HenhouseId"].Value);
                EditHenhouseForm editForm = new EditHenhouseForm(henhouseId);
                if (editForm.ShowDialog() == DialogResult.OK)
                {
                    LoadHenhouses();
                }
            }
        }

        private void btnDeleteHenhouse_Click(object sender, EventArgs e)
        {
            if (dgvHenhouses.SelectedRows.Count > 0)
            {
                int henhouseId = Convert.ToInt32(dgvHenhouses.SelectedRows[0].Cells["HenhouseId"].Value);
                DialogResult dialogResult = MessageBox.Show($"确定要删除ID为 {henhouseId} 的鸡舍吗？", "提示", MessageBoxButtons.YesNo);
                if (dialogResult == DialogResult.Yes)
                {
                    using (SqlConnection conn = DBConnection.CreateConnection())
                    {
                        try
                        {
                            DBConnection.Open(conn);
                            SqlCommand cmd = new SqlCommand($"DELETE FROM Henhouse WHERE HenhouseId = {henhouseId}", conn);
                            cmd.ExecuteNonQuery();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("删除鸡舍时发生错误: " + ex.Message);
                        }
                        finally
                        {
                            DBConnection.Close(conn);
                        }
                    }
                    LoadHenhouses();
                }
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
